<!DOCTYPE html>
<html>
<head>
    <title>创艺装饰</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="./component/pear/css/pear.css"/>
    <link rel="stylesheet" href="./admin/css/admin.css"/>
    <link rel="stylesheet" href="./admin/css/admin.dark.css"/>
    <link rel="stylesheet" href="./admin/css/variables.css"/>
    <link rel="stylesheet" href="./admin/css/reset.css"/>
    <link rel="stylesheet" href="./admin/css/formSelects-v4.css">
    <style>
        .layui-form-label2 {
            position: relative;
            float: left;
            display: block;
            padding: 9px 15px;
            width: auto;
            font-weight: 400;
            line-height: 20px;
            text-align: right
        }
    </style>
</head>
<!-- 结 构 代 码 -->
<body class="layui-layout-body pear-admin">
<!-- 布 局 框 架 -->
<div class="layui-layout layui-layout-admin">
    <!-- 顶 部 样 式 -->
    <div class="layui-header">
        <!-- 菜 单 顶 部 -->
        <div class="layui-logo">
            <!-- 图 标 -->
            <img class="logo">
            <!-- 标 题 -->
            <span class="title"></span>
        </div>
        <!-- 顶 部 左 侧 功 能 -->
        <ul class="layui-nav layui-layout-left">
            <li class="collapse layui-nav-item"><a href="#" class="layui-icon layui-icon-shrink-right"></a></li>
            <li class="refresh layui-nav-item"><a href="#" class="layui-icon layui-icon-refresh-1" loading=600></a></li>
        </ul>
        <!-- 多 系 统 菜 单 -->
        <div id="control" class="layui-layout-control"></div>
        <!-- 顶 部 右 侧 菜 单 -->
        <ul class="layui-nav layui-layout-right">
            <!--					<li class="layui-nav-item layui-hide-xs"><a href="#" class="menuSearch layui-icon layui-icon-search"></a></li>-->
            <li class="layui-nav-item" id="clearCache"><a href="javascript:void(0)"
                                                          class="layui-icon layui-icon-refresh-1"></a></li>
            <li class="layui-nav-item layui-hide-xs message"></li>
            <li class="layui-nav-item layui-hide-xs"><a href="#"
                                                        class="fullScreen layui-icon layui-icon-screen-full"></a></li>
            <li class="layui-nav-item user">
                <!-- 头 像 -->
                <a class="layui-icon layui-icon-username" href="javascript:;"></a>
                <!-- 功 能 菜 单 -->
                <dl class="layui-nav-child">
                    <dd><a id="userInfo">基本资料</a></dd>
                    <dd><a id="cpListDiv" user-menu-title="切换公司">切换公司</a></dd>
                    <dd><a href="javascript:void(0);" class="logout">注销登录</a></dd>
                </dl>
            </li>
            <!-- 主 题 配 置 -->
            <li class="layui-nav-item setting"><a href="#" class="layui-icon layui-icon-more-vertical"></a></li>
        </ul>
    </div>
    <!-- 侧 边 区 域 -->
    <div class="layui-side layui-bg-black">
        <!-- 菜 单 顶 部 -->
        <div class="layui-logo">
            <!-- 图 标 -->
            <img class="logo">
            <!-- 标 题 -->
            <span class="title"></span>
        </div>
        <!-- 菜 单 内 容 -->
        <div class="layui-side-scroll">
            <div id="side"></div>
        </div>
    </div>
    <!-- 视 图 页 面 -->
    <div class="layui-body">
        <!-- 内 容 页 面 -->
        <div id="content"></div>
    </div>
    <!-- 页脚 -->
    <div class="layui-footer layui-text"></div>
    <!-- 遮 盖 层 -->
    <div class="pear-cover"></div>
    <!-- 加 载 动 画 -->
    <div class="loader-wrapper">
        <!-- 动 画 对 象 -->
        <div class="loader"></div>
    </div>
</div>
<!-- 移 动 端 便 捷 操 作 -->
<div class="pear-collapsed-pe collapse">
    <a href="#" class="layui-icon layui-icon-shrink-right"></a>
</div>

<!--切换公司-->
<form class="layui-form" action="" lay-filter="cpDialog" id="cpDialog" style="display: block">
    <div id="cpTree" class="demo-tree"></div>
</form>

<!--个人资料-->
<form class="layui-form " action="" lay-filter="userDialog" id="userDialog" style="display: none">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 25px;">
            <div class="text-center layui-text">
                <div class="user-info" id="userInfoHead" style="text-align: center">
                    <img src="../../admin/images/avatar.jpg" id="userAvatar" width="55px" height="55px"
                         style="border-radius:50px"
                         alt="">
                </div>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">姓名：</label>
        <div>
            <span class="layui-form-label2" id="d_userName"></span>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">电话：</label>
        <div>
            <span class="layui-form-label2" id="d_loginName"></span>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">公司：</label>
        <div>
            <span class="layui-form-label2" id="d_cpName"></span>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">岗位：</label>
        <div>
            <span class="layui-form-label2" id="d_roleName"></span>
        </div>
    </div>
</form>

<!-- 依 赖 脚 本 -->
<script src="./component/layui/layui.js"></script>
<script src="./component/pear/pear.js"></script>
<script src="./common/common-config.js"></script>
<script src="./component/pear/module/extends/jsencrypt.js"></script>
<script src="./component/pear/module/extends/formSelects-v4.js"></script>
<script src="./component/layui/modules/echarts.min.js"></script>
<!-- 框 架 初 始 化 -->
<script>
    layui.use(['jquery', 'admin', 'popup', 'pub', 'http', 'tree', 'shopx','tabPage'], function () {
        var admin = layui.admin;
        var pub = layui.pub;
        var http = layui.http;
        var tree = layui.tree;
        var shopx = layui.shopx;
        var tabPage = layui.tabPage;
        var $ = layui.jquery;

        //刷新关闭弹窗
        


        var userInfo = shopx.getUserInfo();
        if (userInfo.cpList.length > 1) {
            $("#cpListDiv").show();
        } else {
            $("#cpListDiv").hide();
        }

        //个人资料
        $("#userInfo").click(function () {
            var userInfo = shopx.getUserInfo();
            $("#d_userName").text(userInfo.userName);
            $("#d_loginName").text(userInfo.loginName);
            $("#d_cpName").text(userInfo.cpName);
            $("#d_roleName").text(userInfo.roleNames);
            layer.open({
                title: '个人资料',
                type: 1,
                skin: 'layui-layer-border',
                area: ['400px', '480px'],
                content: $('#userDialog'),
                offset: 'rt',
                maxmin: false,
                shade: false,
                minStack: false, // 最小化不堆叠在左下角
                id: 'page-2', // 定义 ID，防止重复弹出
            });
        });

        //清缓存
        $("#clearCache").click(function () {
            /** 字典项清空 */
            http.clearDict();
        });

        //公司切换
        $("#cpListDiv").click(function () {
            var cpIds = "";
            $(userInfo.cpList).each(function (idx, item) {
                cpIds += item.cpId + ",";
            });
            if (!pub.isEmpty(cpIds)) {
                cpIds = cpIds.substr(0, cpIds.length - 1);
            }
            var model2 = {
                cpId: cpIds
            }
            http.postRequestNoLogin("/sysCompany/selectCpTree", 'POST', model2, function suc(data) {
                // console.info(JSON.stringify(data.rspData));
                tree.render({
                    elem: '#cpTree'
                    , data: data.rspData
                    , onlyIconControl: true  //是否仅允许节点左侧图标控制展开收缩
                    , click: function (obj) {
                        // layer.msg(JSON.stringify(obj.data));
                        if (obj.data.level == 3) {
                            var model = {}
                            http.postRequest('/sysUser/changeCp?cpId=' + obj.data.id, 'POST', model, function finish(data) {
                                var tokenId = shopx.getUserTokenId();
                                shopx.setUserTokenId(tokenId);
                                shopx.setUserInfo(data.rspData);
                                location.reload();
                            }, null, false, false);
                        }
                    }
                });
                layer.open({
                    title: '选择登录企业',
                    type: 1,
                    skin: 'layui-layer-border',
                    area: ['400px', '480px'],
                    content: $('#cpDialog'),
                    maxmin: false,
                    shade: false,
                    minStack: false, // 最小化不堆叠在左下角
                    id: 'page-1', // 定义 ID，防止重复弹出
                });
            });
        });

        admin.setConfigurationPath("config/pear.config.yml");

        admin.render();

        admin.logout(function () {
            layer.confirm('提示，确认退出吗？', {
                btn: ['确定', '取消']
            }, function () {
                //清空缓存
                localStorage.clear();
                location.href = "./login.html";
            }, function () {
            });
        });

        //载入字典项到缓存
        http.getDict();
        //获取有权限的按钮，写入缓存
        http.getUserFunc();

        // 初始化tabPage实例
        var tabInstance = tabPage.render({
            elem: 'pear-admin-content',
            multiTab: true
        });

        // 增强的页面卸载处理
        function handleBeforeUnload() {
            // 同步执行关闭操作
            try {
                // 1. 关闭所有tab页
                tabInstance.removeTab();
                
                // 2. 关闭所有layer弹窗
                layer.closeAll();
                
                // 3. 清除所有相关存储
                sessionStorage.clear();
                localStorage.removeItem('__parentActive');
                
                // 4. 关闭所有子窗口
                if(window.childWindows){
                    window.childWindows.forEach(win => {
                        try {
                            if(!win.closed) {
                                win.postMessage('FORCE_CLOSE', location.origin);
                                win.close();
                            }
                        } catch(e){
                            console.error('窗口关闭失败:', e);
                        }
                    });
                    window.childWindows = [];
                }
            } catch(e) {
                console.error('卸载处理异常:', e);
            }
        }

        // 绑定事件监听
        window.addEventListener('beforeunload', handleBeforeUnload);
        window.addEventListener('pagehide', handleBeforeUnload); // 兼容Safari
        window.addEventListener('unload', handleBeforeUnload);

        // 防止多次绑定
        if(window.__hasBindUnload) return;
        window.__hasBindUnload = true;
    })
</script>
</body>
</html>